//@ts-ignore
import { addendmd, getabsoluteindex, getbaseurl } from "./getbaseurl";
import { hashrouter } from "./hashrouter";
//@ts-ignore
import { isrelativepath } from "./isrelativepath";
import { pathrouter } from "./pathrouter";
//@ts-ignore
import { urlclearhashandsearch } from "./urlclearhashandsearch";
export function resolvemdpathfromurl(): string {
    if (process.env.PRERENDER) {
        const router = pathrouter;
        const baseurl = getbaseurl();
        const params = router.getparams();
        let path: string;
        if (Reflect.has(params, "md")) {
            path = Reflect.get(params, "md");
        } else {
            path = getabsoluteindex();
        }
        path = path.endsWith(".md") ? path : path + ".md";
        if (isrelativepath(path)) {
            path = new URL(path, baseurl).toString();
        }

        path = urlclearhashandsearch(path);
        path = addendmd(path);
        return path;
    } else {
        const router = hashrouter;
        const baseurl = getbaseurl();
        let path: string;
        const params = router.getparams();
        // console.log(params)
        if (Reflect.has(params, "md")) {
            path = Reflect.get(params, "md");
        } else {
            path =
                location.hash === "" || location.hash === "#"
                    ? getabsoluteindex()
                    : location.hash.replace("#", "");
        }

        path = path.endsWith(".md") ? path : path + ".md";
        if (isrelativepath(path)) {
            path = new URL(path, baseurl).toString();
        }

        path = urlclearhashandsearch(path);
        path = addendmd(path);
        return path;
    }
}
